package com.eascs.admin.common.utils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.math.BigDecimal;

/**
 * 公共的toString 类
 * @author CXW
 * 2017-05-16
 */
public class EntityToStringUtils  implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4520612843088877301L;

	@SuppressWarnings("rawtypes")
	public String toString() {
		StringBuffer sb = new StringBuffer();
		try {
			Class t = this.getClass();
			Field[] fields = t.getDeclaredFields();
			sb.append("{");
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				field.setAccessible(true);
				sb.append(field.getName());
				sb.append(":");
				if (field.getType() == Integer.class) {
					sb.append(field.getInt(this));
				} else if (field.getType() == Long.class) {
					sb.append(field.getLong(this));
				} else if (field.getType() == Boolean.class) {
					sb.append(field.getBoolean(this));
				} else if (field.getType() == char.class) {
					sb.append(field.getChar(this));
				} else if (field.getType() == Double.class) {
					sb.append(field.getDouble(this));
				} else if (field.getType() == Float.class) {
					sb.append(field.getFloat(this));
				} else if (field.getType() == BigDecimal.class) {
					if (field.get(this) == null || field.get(this).equals("")) {
						sb.append(new BigDecimal("0.00"));
					} else
						sb.append(new BigDecimal(field.get(this).toString()).floatValue());
				} else
					sb.append(field.get(this));
				sb.append(",");
			}
			sb.append("}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.delete(sb.length() - 2, sb.length() - 1).toString();
	}
}
